package cn.lf.android.lawfirm.distribution;


import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import cn.lf.android.lawfirm.OrdinaryBaseActivity;
import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.distribution.controller.DistributionListConroller;
import cn.lf.android.lawfirm.distribution.modle.Distribution;
import cn.lf.android.lawfirm.distribution.view.DistributionAdapter;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.SPUtil;
import cn.lf.android.lawfirm.view.OptionDialog;

import static cn.lf.android.lawfirm.MyApplication.context;
import static cn.lf.android.lawfirm.R.id.img_left;
import static cn.lf.android.lawfirm.R.id.rl_search;

public class DistributionListActivity extends OrdinaryBaseActivity
        implements DistributionListConroller.DistributionListCallback, DistributionListConroller.DistributionDeleteCallback {
    private ListView mListView;
    private SmartRefreshLayout refreshLayout;
    private DistributionAdapter mDistributionAdapter;
    private List<Distribution> list = new ArrayList<Distribution>();

    private DistributionListConroller mDistributionListConroller;
    private int pageIndex = 1;
    /**
     * 待审批（notYet）、已审批（pass）、已退回（sendBack）和全部（all）（必填默认为待审批）（必填）
     **/
    private String state = "notYet";

    public static Boolean isRefresh = false;

    @Override
    public int getChildView() {
        return R.layout.activity_undertake_list;
    }

    @Override
    public void initViewId() {
        initTitle();
        refreshLayout = (SmartRefreshLayout) findId(R.id.refreshLayout);
        mListView = (ListView) findId(R.id.lv_list);
        mDistributionListConroller = new DistributionListConroller(this, this, this);
        mDistributionAdapter = new DistributionAdapter(this, list, 0, mDistributionListConroller);
        mListView.setAdapter(mDistributionAdapter);
        tvSearch.setVisibility(View.VISIBLE);
        mSearch.setVisibility(View.GONE);
        setLisenters();
    }

    @Override
    public void refreshLoad() {

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case img_left:
                finish();
                break;
            case rl_search:
                Intent intent = new Intent(this, DistributionSearchListActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }


    private void initTitle() {
        mImgLeft.setVisibility(View.VISIBLE);
        mImgLeft.setOnClickListener(this);
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setText("案源分派");
    }

    private void setLisenters() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(0);//0秒
                pageIndex = 1;
                list.clear();
                mDistributionListConroller.onDistributList("", String.valueOf(pageIndex), state);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(2000);
                pageIndex++;
                mDistributionListConroller.onDistributList("", String.valueOf(pageIndex), state);
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Intent intent = new Intent(ChangeApprovalListActivity.this, CaseDetailActivity.class);
//                intent.putExtra("caseID", list.get(position).caseID);
//                startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {
        getData();
        SPUtil spUtil = new SPUtil(context);
        DebugLog.e("Authorization---" + spUtil.getAccessToken());
        mDistributionListConroller.onDistributList("", String.valueOf(pageIndex), state);
    }


    private void getData() {
    }

    public void getDelete(int position) {
        requestOutOrder(position);
    }

    /**
     * 删除提示
     */
    public void requestOutOrder(final int position) {
        OptionDialog dialog = new OptionDialog(this);
        dialog.setAnimationEnable(true);
        View view = LayoutInflater.from(this).inflate(
                R.layout.dialog_order_done, null);
        TextView prompt = (TextView) view.findViewById(R.id.prompt);
        prompt.setText("是否确认删除？");
        dialog.setCustomView(view);

        dialog.setPositiveListener("确定", new OptionDialog.OnPositiveListener() {
            @Override
            public void onClick(OptionDialog dialog) {
                mDistributionListConroller.onDeleteDistribut(list.get(position).LsysCaseID);
                dialog.dismiss();
            }
        });
        dialog.setNegativeListener("取消", new OptionDialog.OnNegativeListener() {

            @Override
            public void onClick(OptionDialog dialog) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isRefresh) {
            if (null != refreshLayout) {
                refreshLayout.autoRefresh();
                isRefresh = false;
            }
        }
    }

    @Override
    public void onDistributionListuccessed(List<Distribution> beans) {
        list.addAll(beans);
        mDistributionAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDistributionListFailed() {

    }

    @Override
    public void onDistributionDeleteSuccessed(String beans) {
        pageIndex = 1;
        list.clear();
        mDistributionListConroller.onDistributList("", String.valueOf(pageIndex), state);
    }

    @Override
    public void onDistributionDeleteFailed() {

    }
}

